<?php
/**
 * Created by PhpStorm.
 * User: xushengbin
 * Date: 2017/12/13
 * Time: AM12:12
 */

namespace app\components;

use Yii;
use yii\helpers\FileHelper;

class UtilComponent extends \yii\base\Component
{
    public $cookieFile;

    public function init()
    {
        parent::init();
        $cookiePath = \Yii::$app->runtimePath . '/cookie';
        FileHelper::createDirectory($cookiePath);
        $this->cookieFile = $cookiePath . '/cookie_' .Yii::$app->session->getId() . '.txt';
    }

    /**
     * @param $path
     * @param $data
     * @param bool $withCookie 请求时是否需要带上cookie
     * @param bool $useMask 是否返回假数据。
     * @return mixed
     */
    public function postJson($path, $data, $withCookie = true, $useMask = false)
    {
        if ($useMask && false) {
            $maskData = $this->simulateData($path, $data);
            return $maskData;
        }

        \Yii::info('--------start-------', 'Util');
        \Yii::info('path:' . $path, 'Util');

        $ch = curl_init();
        $data = json_encode($data);

        \Yii::info($data, 'Util');

        $url = \Yii::$app->params['apiHost'] . $path;

        if ($withCookie) {
            \Yii::info('--------use  cookie file-------' . $this->cookieFile, 'Util');
            curl_setopt($ch, CURLOPT_COOKIEFILE, $this->cookieFile);
        }

        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        curl_setopt($ch, CURLOPT_TIMEOUT, 10);
        curl_setopt($ch, CURLOPT_HEADER, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, [
            'Content-Type: application/json'
        ]);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

        if ($path == '/loginIn' || $path == '/sendMsg') {
            \Yii::info('--------save cookie file-------' . $this->cookieFile, 'Util');
            curl_setopt($ch, CURLOPT_COOKIEJAR, $this->cookieFile);
        }
        $server_output = curl_exec($ch);
        curl_close($ch);

        \Yii::info($server_output, 'Util');
        \Yii::info('--------end-------', 'Util');
        if (empty($server_output)) {
           return false;
        }
        $data = explode("\r\n\r\n", $server_output);
        $header = $data[0];
        $body = $data[1];
        $return = json_decode($body, 1);
        return $return;
    }

    public function simulateData($path, $postData)
    {
        switch ($path) {
            case '/operateDevice':
                switch ($postData['operations']) {
                    //获取设备时间
                    case '$PHKJ#IMEI#GET#TIME#0#12*':
                        $json = <<<'EOF'
{"returnCode":"200","message":"成功","data":{"operationResult":"$PHKJ#IMEI#GET#TIME#20171114123000#12*"}}
EOF;
                        break;
                    default:
                        $json = <<<'EOF'
{"returnCode":"200","message":"成功","data":{"operationResult":"$PHKJ#863987032454450#GET#STATUS#0110/3575257055752570/12001230/13001330/14001430/15001530/16001640#12*#13.22#20#13.22#20#13.22#20"}}
EOF;
                        break;
                }
                break;
            case 'deleteDevice':
                $json = <<<'EOF'
{"returnCode":"200","message":"成功","data":{"operationResult":"$PHKJ#IMEI#GET#TIME#20171114123000#12*"}}
EOF;
                break;
            default:
                exit('请配置模拟返回的数据');
        }
        $return = json_decode($json, 1);
        return $return;
    }
}